/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.giant.services.views;

import com.giant.services.IServicioPersistenciaLocal;
import com.giant.services.ServicioPersistencia;
import com.giant.views.InOrderV;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author taticay
 */
@Stateless
public class ServiceOrderV implements ServiceOrderVLocal {

    //-----------------------------------------------------------
    // Atributos
    //-----------------------------------------------------------
    /**
     * Interface con referencia al servicio de persistencia en el sistema
     */
    @EJB
    private IServicioPersistenciaLocal persistencia;

    //-----------------------------------------------------------
    // Constructor
    //-----------------------------------------------------------
    /**
     * Constructor de la clase sin argumentos
     */
    public ServiceOrderV() {
        persistencia = new ServicioPersistencia();
    }

    //-----------------------------------------------------------
    // Métodos
    //-----------------------------------------------------------
    /**
     * Devuelve todos los vendedores del sistema
     *
     * @return vendedores Vendedores del sistema
     */
    @Override
    public List<InOrderV> getOrderV() {
        return persistencia.findAll(InOrderV.class);
    }

    /**
     * Devuelve todos los vendedores del sistema
     *
     * @return vendedores Vendedores del sistema
     */
    @Override
    public List<InOrderV> getOrderVFilter(String a, Map<String, String> map) {
        return persistencia.executeSql(a, map);
    }

    @Override
    public InOrderV orderById(long id) {
        InOrderV v = (InOrderV) persistencia.findById(InOrderV.class, id);

        return v;

    }

    @Override
    public void replicateOrder(InOrderV orderV) throws Exception {
        //TODO ADICIONAR TRANSACCIONALIDAD
        
        String oldId=orderV.getIdOrder()+"";
        
        //Crear orden
        String createOrder="INSERT INTO in_order(id_order, id_pos, id_person, num_doc, date_order, id_shipment, payment, status, annuled, date_annuled, reason_annu,"
                + " id_person_annu, observations) select null, id_pos, id_person, num_doc, CURDATE(), id_shipment, 0, 0, 0, null, null, null, "
                + " 'Replicated order' from in_order where id_order="+oldId;
        
        persistencia.executeGenericSQLAdd(createOrder);
        //Traer ultima orden
        
        String getOrder="SELECT ifnull(max(id_order),0) as max FROM in_order";
        List<Long> temp=persistencia.executeGenericSQL(getOrder);
        Long newId=null;
        if(!temp.isEmpty())
        {
            newId=temp.get(0);
        
        
        //Crear detalles
        String details="INSERT INTO in_det_order(id_det_order, id_order, id_pos, id_brand, id_line, id_class, id_catalog, id_color, id_size, sku, price_unit, "
                + "quantity, status, annuled) SELECT null, "+newId+", id_pos, id_brand, id_line, id_class, id_catalog, id_color, id_size, sku, price_unit,"
                + " quantity, status, annuled FROM in_det_order WHERE id_order="+oldId;
        
        persistencia.executeGenericSQLAdd(details);
        }
    }
}
